// pages/user/index.js
import Dialog from "../../dist/dialog/vtu-index";
const MAX_LIMIT = 40;
const db = wx.cloud.database();
const TmplId ='s21gXnzWQX301uB7BY2SPZDPLt6Hu6hHF1EBc8nMgqQ';
//var getSum =""
Page({
  data: {
    userinfo:{},
    blogList: [],
    totalList: [],
    value: true,
    Time:0
  },

  onLoad: function() {
    this._getListByCloudFn()
  },
  
  _getListByCloudFn() {
    wx.cloud.callFunction({
      name: 'blog',
      data: {
        $url: 'getListByOpenid',
        start: this.data.blogList.length,
        count: MAX_LIMIT
      }
    }).then((res) => {
      //console.log(res.result.length)
      this.setData({
        blogList: res.result.length
      })
    })
    wx.stopPullDownRefresh()
  
  },
  /*
  alert1: function() {
    Dialog().alert({
      title: '友情提示',
      content: '开启后learn每天会给您发送来学习的消息哦'
    })
  },*/

  /* 订阅授权 */
  onSubscribe: function(e) {
    //const item = e.currentTarget.dataset.item;
    wx.requestSubscribeMessage({
      tmplIds: [TmplId],
      success(res) {
        //console.log(res.errMsg)
        if (res.errMsg === 'requestSubscribeMessage:ok') {
          wx.cloud.callFunction({
              //通过调用云函数，实现用户点击允许我们发送订阅消息，
              //将该数据订阅保存到数据库，以便在满足条件的时候发送给用户
              name: 'subscribeMessage',
              data: {
                //data: item,
                templateId: TmplId,
                //这个是给用户发送订阅消息后，用户点击订阅消息进入小程序的相关页面，一定要是在线的才可以
                page:'pages/learn/learn',
              },
            })
            .then(() => {
              /*wx.showToast({
                title: '订阅成功',
                icon: 'success',
                duration: 2000,
              });*/
              /*发送消息开始*/
              wx.cloud.callFunction({
                name: 'subscribeMessagesend',
                success(res){
                  wx.showToast({
                    title: '打卡成功',
                    icon: 'success',
                    duration: 2000,
                  });
                  console.log(res)
                },
                fail(re){
                  console.log(re)
                }
              })
              /*发送消息结束*/
            })
            .catch(() => {
              wx.showToast({
                title: '订阅失败',
                icon: 'success',
                duration: 2000,
              });
            });
        }
      },
      fail(){
        console.log(re)
      },
    });
  },

  onShow(){
    const totalList=wx.getStorageSync("totalList");
    this.setData({totalList});
    var learnTime = 0.00
    for (var i = 0; i < totalList.length; i++) {
      var getSum = Math.floor(totalList[i]*100)/100
      //console.log(getSum)
      learnTime = Math.floor((learnTime + getSum)*100)/100
    }
    //console.log(learnTime)
    this.setData({
      Time:learnTime
    })
  },

  onPullDownRefresh: function() {
    //将当前列表清空，从云空间重新从0开始取
    this.setData({
      blogList: []
    })
    this._getListByCloudFn()

    const totalList=wx.getStorageSync("totalList");
    this.setData({totalList});
  },

  closeCustomDialog: function() {
    Dialog("Vtu-Custom-Dialog").close()
  },
  

  onShareAppMessage: function () {
    return {
      title: '高颜值自习神器 Learn 随处',
      imageUrl: 'cloud://bluewave-7co1p.626c-bluewave-7co1p-1301720800/image/QR Code.jpg'
    }
  }
})